package cn.tedu.zx.ams.adapter.controller;


import cn.tedu.zx.ams.service.AdminService;
import cn.tedu.zx.common.restful.JsonPage;
import cn.tedu.zx.common.restful.JsonResult;
import cn.tedu.zx.pojo.admin.parm.AdminAccountUpdateParam;
import cn.tedu.zx.pojo.admin.parm.AdminAddNewParam;
import cn.tedu.zx.pojo.admin.parm.AdminDataUpdateParam;
import cn.tedu.zx.pojo.admin.parm.AdminLoginInfoParam;
import cn.tedu.zx.pojo.admin.vo.AdminDataVO;
import cn.tedu.zx.pojo.admin.vo.AdminDetailVO;
import cn.tedu.zx.pojo.admin.vo.AdminListItemVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping("/admin")
@Api(tags = "01. 管理员管理模块")
@Slf4j
@Validated
public class AdminController {

    @Autowired
    private AdminService adminService;

/*    @Autowired
    PasswordEncoder passwordEncoder;*/

    @PostMapping("/add-new")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAnyAuthority('/admin/insert')")
    public JsonResult<Void> addNew(@Valid AdminAddNewParam adminAddNewParam) {
        log.debug("开始处理【添加管理员】的请求，参数：{}", adminAddNewParam);
        adminService.addNew(adminAddNewParam);
        return JsonResult.ok();
    }


    @PostMapping("/delete")
    @ApiOperation("根据ID删除管理员")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAnyAuthority('/admin/delete')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "提交的管理员ID必须大于0", required = true, dataType = "Long")
    })
    public JsonResult<Void> delete(@Range(min = 1, message = "根据ID删除管理员失败,请提交合法的ID值") @RequestParam Long id) {
        log.debug("开始处理【根据ID删除管理员】的请求，参数：{}", id);
        adminService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改管理员信息")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAnyAuthority('/admin/update')")
    public JsonResult<Void> updateData(@Valid AdminDataUpdateParam adminDataUpdateParam) {
        adminService.updateDataById(adminDataUpdateParam);
        return JsonResult.ok();
    }

    @PostMapping("/updateIsEnable")
    @ApiOperation("修改管理员状态")
    @ApiOperationSupport(order = 301)
    @PreAuthorize("hasAnyAuthority('/admin/update')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "isEnable", value = "管理员状态", required = true, dataType = "Integer")
    })
    public JsonResult<Void> updateIsEnable(@RequestParam Long id, @RequestParam Integer isEnable) {
        log.debug("开始执行【修改管理员状态】业务，id：{}，isEnable：{}",id,isEnable);
        adminService.updateIsEnableById(id, isEnable);
        return JsonResult.ok();
    }

    @PostMapping("/updateAccount")
    @ApiOperation("修改管理员账户")
    @ApiOperationSupport(order = 302)
    @PreAuthorize("hasAnyAuthority('/admin/update')")
    public JsonResult<Void> updateAccount(@Valid AdminAccountUpdateParam adminAccountUpdateParam) {
        log.debug("开始执行【修改管理员账户】业务，adminAccountUpdateParam：{}",adminAccountUpdateParam);
        adminService.updateAccount(adminAccountUpdateParam);
        return JsonResult.ok();
    }


    @GetMapping("/adminData")
    @ApiOperation("获取管理员信息")
    @ApiOperationSupport(order = 400)
//    @PreAuthorize("hasAnyAuthority('/admin/read')")
    @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")
    public JsonResult<AdminDataVO> login(@RequestParam String username) {
        log.debug("开始处理【获取管理员信息】的请求");
        AdminDataVO adminDataVO = adminService.adminData(username);
        return JsonResult.ok(adminDataVO);
    }

    @GetMapping("/list")
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 401)
    @PreAuthorize("hasAnyAuthority('/admin/read')")
    public JsonResult<JsonPage> list(Integer pageNum, Integer PageSize) {
        log.debug("开始处理【查询管理员列表】的业务");
        return JsonResult.ok(adminService.list(pageNum, PageSize ));
    }

    @PostMapping("/detail")
    @ApiOperation("查询管理员详情")
    @ApiOperationSupport(order = 402)
    @PreAuthorize("hasAnyAuthority('/admin/read')")
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "Long")
    public JsonResult<AdminDetailVO> detail(@RequestParam Long id){
        log.debug("开始处理【查询管理员详情】的业务");
        return JsonResult.ok(adminService.detail(id));
    }
}
